<?php

class Model_user extends CI_Model {
    
    public function __construct() {
        parent::__construct();
        $this->load->helper('string');
        $this->path = realpath(APPPATH.'../img/profpic');
        $this->filename = random_string('alnum', 32);
    }

    function validate() {
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        $q1 = $this->db->query("SELECT * FROM tb_user WHERE id_user='$username' AND pass='$password'");

        if ($q1->num_rows == 1) {
            $row = $q1->row();
            $creds = array(
                'id_user' => $row->id_user,
                'nama' => $row->nama,
                'level' => $row->level
            );
            $this->session->set_userdata($creds);
        } else {
            $creds = null;
        }

        return $creds;
    }

    function auth() {
        if (isset($this->session->userdata['level'])) {
            $authenticated = 1;
        } else {
            $authenticated = 0;
        }
        return $authenticated;
    }

    function cek_member($q,$id) {
        $query = $this->db->query("SELECT id_user, nama, profpic FROM tb_user WHERE id_user NOT IN (SELECT id_user FROM tb_member WHERE id_group = $id) AND id_user NOT IN (SELECT owner FROM tb_group WHERE id_group = $id) AND nama LIKE '%$q%'");
        return $query->result();
    }

    function user_creds() {
        $id = $this->session->userdata['id_user'];
        $query = $this->db->query("SELECT * FROM tb_user WHERE id_user = $id");
        return $query->row();
    }

    function edit_profpic() {
        //        upload gambar
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->path,
            'file_name' => $this->filename
        );
        $this->load->library('upload', $config);
        $upload = $this->upload->do_upload('photo');

//        masukin database
        $name = $_FILES["photo"]["name"];
        $gambar = end(explode(".", $name));

        $posting_data = array(
            'profpic' => $this->filename . "." . $gambar
        );

        $this->db->where('id_user', $this->session->userdata('id_user'));
        $update = $this->db->update('tb_user', $posting_data);
        return $update;
    }
    
    function edit_pass(){
        $id_user = $this->session->userdata['id_user'];
        $oldone = md5($this->input->post('oldone'));
        $newone = md5($this->input->post('newone'));
        $q1 = $this->db->query("SELECT * FROM tb_user WHERE id_user='$id_user' AND pass='$oldone'");

        if ($q1->num_rows == 1) {
            $posting_data = array(
                'pass' => $newone
            );
            $this->db->where('id_user', $this->session->userdata('id_user'));
            $update = $this->db->update('tb_user', $posting_data);
            $brasil = 1;
        } else {
            $brasil = 0;
        }
        return $brasil;
    }
    
    function curProfPic(){
        $id = $this->session->userdata['id_user'];
        $query = $this->db->query("SELECT profpic FROM tb_user WHERE id_user = $id");
        return $query->row();
    }

}

?>
